package com.gitee.huanminabc.utils_tools.security.access;

import com.gitee.huanminabc.utils_tools.security.vo.SecurityResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录失败处理器：返回状态码402
 * @author huanmin
 * @date 2024/1/24
 */

@Component
@Slf4j
public class SecurityAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        // 将字符串渲染到客户端
        SecurityResponse.error(402, e.getMessage(),httpServletResponse);

    }
}
